package com.dxq.oa.utils;

import com.dxq.oa.pub.VueData;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class VerifyCode {

    public static VueData getCode(){
        //创建image对象
        BufferedImage bufferedImage = new BufferedImage(100,50,BufferedImage.TYPE_INT_BGR);

        //创建画板对象，填充内容
        Graphics2D graphics = bufferedImage.createGraphics();

        //设置背景颜色
        graphics.setColor(Color.WHITE);
        graphics.setBackground(Color.WHITE);
        graphics.fillRect(0,0,100,50);

        //创建4位10以内的随机数作为内容填充
        List<Integer> list = new ArrayList<>();

        Random random = new Random();

        for(int i= 0 ;i<4;i++){
            list.add(random.nextInt(10));
        }

        //设置字体，形状，大小，颜色等
        graphics.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,30));

        //创建颜色数组，随机存储颜色

        Color [] c = new Color[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.BLACK};
        String str = new String();
        for(int i = 0 ;i <list.size();i++){
            graphics.setColor(c[random.nextInt(c.length)]);
            str+=list.get(i);

            graphics.drawString(list.get(i)+"",15+(15*i),25);
        }






        //创建响应流，响应浏览器请求
        return new VueData(str,bufferedImage);
    }


}
